<?php
/***********************************************************************
 * @package    IvanEngine
 * @subpackage Framework
 * @author     Ivan <ivanzx@msn.com>
 * @created    2010-06-21
 ***********************************************************************/
if(defined("IVANENGINE_FRAMEWORK_WINDOWMODIFIER"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_FRAMEWORK_WINDOWMODIFIER", true);

require_once (M_CORE_PATH.'Modifier.php');

class CWindowModifier extends CModifier
{
    protected 
        $m_window       = null,
        $m_view         = null,
        $m_attrList     = null,
        $m_isShow       = true,
        $m_isSession    = false,
        $m_isDestroy    = false;
        
    static public $ms_defWin = null;
    
    public function __construct($winName, CView & $view)
    {
        parent::__construct();
        $winMgr = CWindowManager::Get();
        $this->m_window     = $winMgr->GetWindow($winName);
        if( !$this->m_window ){
            if( $winMgr->HasInStorage($winName) ){
                $this->m_isSession = true;
                $this->m_window = $winMgr->GetRoot()->CreateWindow($winName);
            }else{
                throw new CExceptionInvalidArgument('Can not find out \''.
                $winName.'\' window, You must create it before.');
            }
        }
        $this->m_view       = $view;
        $this->m_attrList   = new CMap();
        
        if( !self::$ms_defWin ){
            self::$ms_defWin = new CWindow('static_default_window');
        }
    }
    
    public function __destruct()
    {
        parent::__destruct();
    }
    
    public function Set($attrName, $value)
    {
        if( $this->m_isDestroy )
            return;
            
        if( !method_exists(CWindowManager::Get()->GetRoot(), 'Set'.ucfirst($attrName)) )
        {
            throw new CExceptionInvalidArgument(
                'Windows do not have this property: '.$attrName);
        }
        
        if( $this->m_attrList->HasKey($attrName) )
        {
            $this->m_attrList->Set($attrName, $value);
        }else{
            $this->m_attrList->Add($attrName, $value);
        }
    }
    
    public function UpdateAll()
    {
        if( $this->m_isDestroy )
            return;
            
        $attrs  = CWindow::AttributeKeys();
        if( $this->m_isSession )
        {
            foreach( $attrs as $attr )
            {
                if( $this->m_window->GetValue($attr) != self::$ms_defWin->GetValue($attr) )
                {
                    $this->m_attrList->Add($attr, $this->m_window->GetValue($attr));
                }
            }
        }else{
            foreach( $attrs as $attr )
            {
                $this->m_attrList->Add($attr, $this->m_window->GetValue($attr));
            }
        }
    }
    
    public function Destroy()
    {
        $this->m_isDestroy   = true;
    }
    
    protected function _DoExecute()
    {
        if( $this->m_isDestroy ){
                $this->m_view->AddScript('$(function(){$(\'#'.$this->m_window->GetID().
                '\').remove();});');
            return;
        }
        
        if( $this->m_attrList->IsEmpty() )
            return;
        
        if( !CWindowManager::Get()->HasInStorage($this->m_window->GetName()) )
        {
            $this->m_view->AddScript($this->m_window->Render(false));
        }
            
        $cmdStr = '$(function(){$(\'#'.$this->m_window->GetID().'\')';
        $cmdStr .= '.window(\'option\',{';
        $it     = $this->m_attrList->Begin();
        $cmdStr .= $it->Key().':'.CWindow::FormatToJsValue($it->Key(), $it->Value());
        for( $it->Next(); !$it->IsEnd(); $it->Next() )
        {
            $cmdStr .= ','.$it->Key().':'.
                CWindow::FormatToJsValue($it->Key(), $it->Value());
        }
        $cmdStr .= '});});';
        $this->m_view->AddScript($cmdStr);
    }
}
?>
